<?php

namespace App\Http\Services;

use App\Http\Model\Hint;
use App\Http\Model\NotifyModel;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;

class SendAlluserNotifyService extends CommonServices
{
    private $sendAlluserNotifyModel; //模型
    protected $brocastService;//广播推送服务

    public function __construct()
    {
        $this->sendAlluserNotifyModel = new NotifyModel();
        $this->brocastService = new MsgServices();
    }
    /**
     * 新建推送
     */
    public function insertOrUpnotify($params)
    {
        //处理数据非全员
        if ($params['is_all'] == 0) {
            //数据接收
            $data['send_nums'] = count($params['userids']);
            $data['userids'] = implode(',', $params['userids']); //用户id
            $data['is_all'] = 0; //非全员
        } else {
            $data['is_all'] = 1; //全员
        }
        //处理图文
        if ($params['send_type'] == 1) {
            $data['title'] = empty($params['title']) ? '' : $params['title']; //图文标题
            $data['desc'] = empty($params['desc']) ? '' : $params['desc']; //图文详情
            $data['img_url'] = $params['img_url']; // 图片连接
            $data['send_type'] = 1; //图文
        } else {
            
            $data['send_type'] = 0; //文案
        }
        //共有数据
        $data['send_time'] = $params['sendtime']; //预计发送时间
        $data['min_damion'] = isset($params['min_damion']) ? $params['min_damion'] : 0; //最小钻石数
        $data['max_damion'] = isset($params['max_damion']) ? $params['max_damion'] : 0; //最大钻石数
        $data['action_url'] = isset($params['action_url']) && !empty($params['action_url']) ? $params['action_url'] : ''; //站内跳转链接
        $data['note'] = isset($params['note']) && !empty($params['note']) ? $params['note'] : ''; //备注
        $data['send_content'] = $params['send_content']; //发送文案
        $data['send_status'] = 0;
        $data['notify_type'] = $params['notify_type'];//消息类型1=官方推送 2=游戏官方推送
        //判断是否是添加还是修改
        if(isset($params['notifyid']) && !empty($params['notifyid']))
        {
            $data['update_time'] = Carbon::now()->toDateTimeString();
            $data['id'] = $params['notifyid'];
        }else{
            $data['create_time'] = Carbon::now()->toDateTimeString();
        }
        
        $status = $this->sendAlluserNotifyModel->insertOrUpnotify($data);
        return $status;
    }
    /**
     * 软删除推送消息
     */
    public function deletenotify($params)
    {
        $code = Hint::SUCCESS;
        $msg = '';
        $data = [];
        do {
            if (empty($params['notifyid'])) {
                $code = Hint::PARAM_ERROR;
                $msg = '缺少notifyid';
                break;
            }
            $this->sendAlluserNotifyModel->deleteNotify($params['notifyid'],$params['status']);
        } while (false);

        return compact('code', 'msg', 'data');
    }

    /**
     * 推送服务
     */
    public function brocast($params)
    {
        $is_all = $params['is_all'];//是否全员
        $userids = $params['userids'];//推送id;
        $send_type = $params['send_type'];//推送类型1图文0文案
        $send_content = $params['send_content'];//推送文案
        $title = $params['title'];//推送标题
        $img_url = $params['img_url'];//图片地址
        $action_url = $params['action_url'];//跳转地址
        $notify_type = $params['notify_type'];//推送类型1=官方 2=游戏
        $notifyId = $params['notify_id'];
        $status = $this->brocastService->newSendNotify($is_all,$userids,$send_content,$send_type,$title,$img_url,$action_url,$notify_type);
        //处理数据
        if($status) {
            $update = [
                'send_status' =>1,
                'send_nums' => count($userids),
                'update_time'=> Carbon::now()->toDateTimeString(),
                'id' => $notifyId,
            ];
            $resStatus = $this->sendAlluserNotifyModel->insertOrUpnotify($update);
            //记录log
            $insert['notifyid'] = $params['notify_id'];
            if(empty($userids))
            {
                $insert['userid'] = 0;
            }else{
                $insert['userid'] = implode(',',$userids);
            }
            
            $insert['status'] = 1;//成功
            $insert['note'] = '已推送';
            $insert['create_time'] = Carbon::now()->toDateTimeString();
            $this->sendAlluserNotifyModel->insertNotifyLog($insert);
        }else{
            //更新数据
            $update = [
                'send_status' =>2,
                'update_time'=> Carbon::now()->toDateTimeString(),
                'id' => $notifyId,
            ];
            $resStatus = $this->sendAlluserNotifyModel->insertOrUpnotify($update);
        }
        return $resStatus;
    }
}
